/*    */ package com.zimbra.cs.account.ldap;
/*    */ 
/*    */ import com.zimbra.common.localconfig.KnownKey;
/*    */ import com.zimbra.common.localconfig.LC;
/*    */ import com.zimbra.common.service.ServiceException;
/*    */ import com.zimbra.cs.account.Provisioning;
/*    */ import com.zimbra.cs.account.cache.IMimeTypeCache;
/*    */ import com.zimbra.cs.mime.MimeTypeInfo;
/*    */ import java.util.Collections;
/*    */ import java.util.HashMap;
/*    */ import java.util.List;
/*    */ import java.util.Map;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class LdapMimeTypeCache
/*    */   implements IMimeTypeCache
/*    */ {
/*    */   private List<MimeTypeInfo> mAllMimeTypes;
/*    */   private Map<String, List<MimeTypeInfo>> mMapByMimeType;
/*    */   private long mRefreshTTL;
/*    */   private long mLifetime;
/*    */   
/*    */   public LdapMimeTypeCache()
/*    */   {
/* 40 */     this.mRefreshTTL = (LC.ldap_cache_mime_maxage.intValue() * 60000L);
/*    */   }
/*    */   
/*    */   public synchronized void flushCache(Provisioning prov) throws ServiceException
/*    */   {
/* 45 */     refresh((LdapProv)prov);
/*    */   }
/*    */   
/*    */   public synchronized List<MimeTypeInfo> getAllMimeTypes(Provisioning prov) throws ServiceException
/*    */   {
/* 50 */     refreshIfNecessary((LdapProv)prov);
/* 51 */     return this.mAllMimeTypes;
/*    */   }
/*    */   
/*    */ 
/*    */   public synchronized List<MimeTypeInfo> getMimeTypes(Provisioning prov, String mimeType)
/*    */     throws ServiceException
/*    */   {
/* 58 */     LdapProvisioning ldapProv = (LdapProvisioning)prov;
/*    */     
/* 60 */     refreshIfNecessary(ldapProv);
/* 61 */     List<MimeTypeInfo> mimeTypes = (List)this.mMapByMimeType.get(mimeType);
/* 62 */     if (mimeTypes == null) {
/* 63 */       mimeTypes = Collections.unmodifiableList(ldapProv.getMimeTypesByQuery(mimeType));
/* 64 */       this.mMapByMimeType.put(mimeType, mimeTypes);
/*    */     }
/* 66 */     return mimeTypes;
/*    */   }
/*    */   
/*    */   private void refreshIfNecessary(LdapProv ldapProv) throws ServiceException {
/* 70 */     if (isStale()) {
/* 71 */       refresh(ldapProv);
/*    */     }
/*    */   }
/*    */   
/*    */   private boolean isStale() {
/* 76 */     if (this.mAllMimeTypes == null) {
/* 77 */       return true;
/*    */     }
/* 79 */     return (this.mRefreshTTL != 0L) && (this.mLifetime < System.currentTimeMillis());
/*    */   }
/*    */   
/*    */   private void refresh(LdapProv ldapProv) throws ServiceException {
/* 83 */     this.mAllMimeTypes = Collections.unmodifiableList(ldapProv.getAllMimeTypesByQuery());
/* 84 */     this.mMapByMimeType = new HashMap();
/* 85 */     this.mLifetime = (System.currentTimeMillis() + this.mRefreshTTL);
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/LdapMimeTypeCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */